﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Bussiness;


namespace GUI
{
    public partial class frmTraCuu : Form
    {
        //private List<SachDTO> _sachList;
        private List<TacGiaDTO> _tacGiaList;
        private List<TheLoaiDTO> _theLoaiList;

        public frmTraCuu()
        {
            InitializeComponent();
        }

        private void frmTraCuu_Load(object sender, EventArgs e)
        {
            //SachService bus = new SachService();
            //dgvKetQuaTimKiem.DataSource = bus.LaySachTB();

            tbxMaSach.Enabled = false;
            tbxTenSach.Enabled = false;
            tbxTonItHon.Enabled = false;
            tbxTonNhieuHon.Enabled = false;
            cbxTacGia.Enabled = false;
            cbxTheLoai.Enabled = false;

            // Lay danh sach tac gia
            TacGiaService tgService = new TacGiaService();
            _tacGiaList = tgService.SelectAllTacGias();

            // Load len Combobox Tac gia
            cbxTacGia.DataSource = this._tacGiaList;
            cbxTacGia.DisplayMember = TacGiaColumn.TenTacGia;
            cbxTacGia.ValueMember = TacGiaColumn.IDTacGia;

            //Lay danh sach the loai
            TheLoaiService tlService = new TheLoaiService();
            _theLoaiList = tlService.SelectAllTheLoais();

            // Load len Combobox The loai
            cbxTheLoai.DataSource = this._theLoaiList;
            cbxTheLoai.DisplayMember = TheLoaiColumn.TenTheLoai;
            cbxTheLoai.ValueMember = TheLoaiColumn.MaTheLoai;
        }

        private void chkbxMaSach_CheckedChanged(object sender, EventArgs e)
        {
            if (chkbxMaSach.Checked)
                tbxMaSach.Enabled = true;
            else
                tbxMaSach.Enabled = false;
        }

        private void chkbxTenSach_CheckedChanged(object sender, EventArgs e)
        {
            if (chkbxTenSach.Checked)
                tbxTenSach.Enabled = true;
            else
                tbxTenSach.Enabled = false;
        }

        private void chkbxTonItHon_CheckedChanged(object sender, EventArgs e)
        {
            if (chkbxTonItHon.Checked)
            {
                tbxTonItHon.Enabled = true;
                chkbxTonNhieuHon.Enabled = false;
            }
            else
            {
                tbxTonItHon.Enabled = false;
                chkbxTonNhieuHon.Enabled = true;
            }
        }

        private void chkbxTheLoai_CheckedChanged(object sender, EventArgs e)
        {
            if (chkbxTheLoai.Checked)
                cbxTheLoai.Enabled = true;
            else
                cbxTheLoai.Enabled = false;
        }

        private void chkbxTacGia_CheckedChanged(object sender, EventArgs e)
        {
            if (chkbxTacGia.Checked)
                cbxTacGia.Enabled = true;
            else
                cbxTacGia.Enabled = false;
        }

        private void chkbxTonNhieuHon_CheckedChanged(object sender, EventArgs e)
        {
            if (chkbxTonNhieuHon.Checked)
            {
                tbxTonNhieuHon.Enabled = true;
                chkbxTonItHon.Enabled = false;
            }
            else
            {
                tbxTonNhieuHon.Enabled = false;
                chkbxTonItHon.Enabled = true;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            SachService sser = new SachService();
            string maSach = "";
            string tenSach = "";
            int idTacGia = -1;
            int idTheLoai = -1;
            int tonItHon = -1;
            int tonNhieuHon = -1;
            if (chkbxMaSach.Checked)
                maSach = tbxMaSach.Text.Trim();
            if (chkbxTenSach.Checked)
                tenSach = tbxTenSach.Text.Trim();
            if (chkbxTacGia.Checked)
                idTacGia = cbxTacGia.SelectedIndex + 1;
            if (chkbxTheLoai.Checked)
                idTheLoai = cbxTheLoai.SelectedIndex + 1;
            if (chkbxTonItHon.Checked)
                tonItHon = Convert.ToInt32(tonItHon);
            if (chkbxTonNhieuHon.Checked)
                tonNhieuHon = Convert.ToInt32(tonNhieuHon);

            dgvKetQuaTimKiem.DataSource = sser.TimKiem(maSach, tenSach, idTacGia, idTheLoai, tonItHon, tonNhieuHon);
        }        
    }
}
